﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace ApplicationLibrary.ExtendedControls
{
    [Designer(typeof(MySnapLinesDesigner))]
    public partial class TextFileFolderBrowse : UserControl, ISnapLineControl
    {
        public enum BrowseDialog { Folder, Open, Save } ;

        [DefaultValue("All|*.*")]
        public string Filter { get; set; }

        [DefaultValue(BrowseDialog.Folder)]
        public BrowseDialog BrowseMode { get; set; }

        public string HeaderText { get; set; }

        public bool ShowNewFolderButton { get; set; }

        public Control textBoxValue { get { return txtFile; } }

        public Control labelValue { get { return null; } }

        public bool ReadOnly
        {
            set
            {
                txtFile.Enabled = !value;
                btnBrowse.Enabled = !value;
                if (value)
                    txtFile.Text = string.Empty;
            }
        }

        public new string Text
        {
            get { return txtFile.Text; }
            set { txtFile.Text = value; }
        }

        public TextFileFolderBrowse()
        {
            InitializeComponent();
            ShowNewFolderButton = false;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            switch (BrowseMode)
            {
                case BrowseDialog.Folder:
                    FolderMode();
                    break;

                case BrowseDialog.Open:
                    OpenMode();
                    break;

                case BrowseDialog.Save:
                    SaveMode();
                    break;
            }
        }

        private void FolderMode()
        {
            FolderBrowserDialog fbrDialog = new FolderBrowserDialog();
            fbrDialog.RootFolder = Environment.SpecialFolder.MyComputer;
            fbrDialog.Description = HeaderText;
            fbrDialog.SelectedPath = txtFile.Text;
            fbrDialog.ShowNewFolderButton = ShowNewFolderButton;

            if (fbrDialog.ShowDialog() == DialogResult.OK)
                txtFile.Text = fbrDialog.SelectedPath;
        }

        private void OpenMode()
        {
            OpenFileDialog fbrDialog = new OpenFileDialog();
            fbrDialog.AutoUpgradeEnabled = true;
            fbrDialog.CheckFileExists = true;
            fbrDialog.Multiselect = false;
            fbrDialog.Filter = Filter;

            if (fbrDialog.ShowDialog() == DialogResult.OK)
                txtFile.Text = fbrDialog.FileName;
        }

        private void SaveMode()
        {
            SaveFileDialog fbrDialog = new SaveFileDialog();
            fbrDialog.AutoUpgradeEnabled = true;
            fbrDialog.CheckFileExists = false;
            fbrDialog.Filter = Filter;

            if (fbrDialog.ShowDialog() == DialogResult.OK)
                txtFile.Text = fbrDialog.FileName;
        }
    }
}